n=0
x=int(input())
if x > 999 and x < 10000:
    for i1 in range(4):
        for i2 in range(i1+1, 5):
            j=x%10**i2//10**i1
            if j != 0 and j != 1:
                for i in range(2, j):
                    if j % i == 0:
                        break
                else:
                     if j > n:
                         n = j
    print(n) 
else:
    print(0)
